#include <iostream>
#include <thread>
#include <unistd.h>
#include "./smart/smart_net_pair.hpp"
#include <string.h>


#define URL "tcp://127.0.0.1:8899"

int server ()
{
    LOG_DEBUG("------------- server start -------------");
    SmartNetPair server(SmartNetPair::NP_SERVER, URL);
    server.init();
    
    std::thread th([&server]{
        while (true) {
            getchar();
            server.send_mesg((uint8_t *)"server\0", 7);
        }
    });

    server.loop_recv_mesg([](uint8_t *buf, uint32_t len){
        LOG_DEBUG("---------------------------> len:%d, buf[%s].", len, buf);
    });

    return 0;
}

int client ()
{
    LOG_DEBUG("------------- client start -------------");
    SmartNetPair client(SmartNetPair::NP_CLIENT, URL);
    client.init();

    std::thread th([&client]{
        while (true) {
            getchar();
            client.send_mesg((uint8_t *)"client\0", 7);
        }
    });

    client.loop_recv_mesg([](uint8_t *buf, uint32_t len){
        LOG_DEBUG("---------------------------> len:%d, buf[%s].", len, buf);
    });

    return 0;
}

int main(const int argc, const char *argv[])
{   
    int rc;
    LOG_DEBUG("main");
    if ((argc == 2) && (strcmp (argv[1], "-s") == 0)) {
        rc = server();
    } else if ((argc == 2) && (strcmp (argv[1], "-c") == 0)) {
        rc = client();
    } else {
        fprintf (stderr, "Usage: %s [-s|-c]\n", argv[0]);
        exit (EXIT_FAILURE);
    }
    exit (rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}